<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/4/21
 * Time: 14:21
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Logout extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('logout_reason')->count();// 获取总条数
            $lists = Db::name('logout_reason')
                ->field('reason_id, reason_name, reason_time')
                ->page($Nowpage,$limits)
                ->order('reason_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加注销原因
     * @return mixed|\think\response\Json
     */
    public function reasonAdd(){
        if(Request::instance()->isPost()){
            $insert = [
                'reason_name' => trim(input('reason_name')),
                'reason_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_logout_reason')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('logout/reasonadd');
    }
    
    /**
     * 编辑注销原因
     * @return mixed|\think\response\Json
     */
    public function reasonEdit(){
        $reason_id = input('reason_id');
        if(Request::instance()->isPost()){
            $res = Db::table('up_logout_reason')
                ->where('reason_id', $reason_id)
                ->update(['reason_name' => trim(input('reason_name'), ' ')]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $reason = Db::table('up_logout_reason')->where('reason_id', $reason_id)->value('reason_name');
        $this->assign([
            'reason_id' => $reason_id,
            'reason_name' => $reason
        ]);
        return $this->fetch('logout/reasonedit');
    }
    
    /**
     * 删除注销原因
     * @return \think\response\Json
     */
    public function reasonDel(){
        $reason_id = input('id');
        $res = Db::table('up_logout_reason')->delete($reason_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}